package ligne2vie.topos.server.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

import ligne2vie.topos.server.bo.Summit;
import ligne2vie.topos.shared.bo.TSummit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * The class {@link SummitServiceLogInTestCase} verifies if the class {@link SummitServiceImpl} correctly works in an
 * independent way from the Web Interface, when the user is logged in.
 * 
 * @author thuguerre
 * @since 1.0.0
 */
public class SummitServiceLogInTestCase extends AbstractSummitServiceTestCase {

	@Before
	public void setUp() {

		super.setUp();

		userLogin(false);
	}

	@After
	public void tearDown() {
		super.tearDown();
	}

	/**
	 * Calls all methods in a logic way in order to test complete life cycle of a {@link Summit}.
	 */
	@Test
	public void validSummitCompleteLifeCycle() {

		// --- creating the summit

		TSummit createdSummit = createSummit();
		verifyCreatedTSummit(createdSummit);

		// --- getting the summit

		Summit foundSummit = service._getSummit(createdSummit.getId(), true);
		verifyCreatedSummit(createdSummit.getId(), foundSummit);

		// --- getting all summits

		List<TSummit> allSummits = service.getAllSummits();

		assertEquals(1, allSummits.size());
		verifyCreatedTSummit(allSummits.get(0));

		// --- editing the summit

		TSummit editedSummit = editSummit(createdSummit.getId());
		verifyEditedTSummit(createdSummit.getId(), editedSummit);

		// --- getting the summit

		Summit foundSummit2 = service._getSummit(createdSummit.getId(), true);
		verifyEditedSummit(createdSummit.getId(), foundSummit2);

		// --- removing the summit

		removeSummit(createdSummit.getId());

		// --- getting the removed summit

		try {

			service.getSummit(createdSummit.getId());
			fail("Should have thrown exception.");

		} catch (RuntimeException ex) {

			assertTrue(ex.getMessage().contains("summit"));
			assertTrue(ex.getMessage().contains("removed"));
		}

		// --- getting the removed summit

		Summit removedSummit = service._getSummit(createdSummit.getId(), false);
		verifyRemovedSummit(createdSummit.getId(), removedSummit, true);

		// --- getting all summits

		assertEquals(0, service.getAllSummits().size());
	}

	/**
	 * Testing if creating a summit with valid information correctly works.
	 */
	@Test
	public void validSummitCreate() {

		// --- creating the summit

		TSummit createdSummit = createSummit();
		verifyCreatedTSummit(createdSummit);

		// --- getting the summit

		Summit foundSummit = service._getSummit(createdSummit.getId(), true);
		verifyCreatedSummit(createdSummit.getId(), foundSummit);
	}

	/**
	 * Testing if editing a summit with valid information correctly works.
	 */
	@Test
	public void validSummitEdit() {

		TSummit createdSummit = createSummit();

		// --- editing the summit

		TSummit editedTSummit = editSummit(createdSummit.getId());
		verifyEditedTSummit(createdSummit.getId(), editedTSummit);

		// --- getting the summit

		Summit editedSummit = service._getSummit(createdSummit.getId(), true);
		verifyEditedSummit(createdSummit.getId(), editedSummit);
	}

	/**
	 * Testing if removing a summit with valid information correctly works.
	 */
	@Test
	public void validSummitRemove() {

		TSummit createdSummit = createSummit();
		assertEquals(1, service.getAllSummits().size());

		// --- removing the summit

		removeSummit(createdSummit.getId());

		// --- getting the removed summit

		try {

			service.getSummit(createdSummit.getId());
			fail("Should have thrown exception.");

		} catch (RuntimeException ex) {

			assertTrue(ex.getMessage().contains("summit"));
			assertTrue(ex.getMessage().contains("removed"));
		}

		// --- getting the removed summit

		Summit removedSummit = service._getSummit(createdSummit.getId(), false);
		verifyRemovedSummit(createdSummit.getId(), removedSummit, false);

		// --- getting all summits

		assertEquals(0, service.getAllSummits().size());
	}
}
